# This coding file conducts additional analyses
# Referenced in the main manuscript and which 
# appear in  Appendix Tables A.6-A.13
# File should should be run sequentially after coding files 1-5

############################################################################
# Calculations for Tables A.6, A.7 and A.8
# Difference and Polarization by Gender
# Private Versus Group Outcomes
############################################################################

#Regressions with fixed effects by outcome preference type
# with standard errors clustered by village

# Note that ALLOUT.XXX Outcome variable and associated crime-specific datasets was created in file 3.Paper_Figures.R

## Individual Fixed effects
S1.VIOL <- lm(ALLOUT.VIOL ~ as.factor(OUTTYPE.VIOL)+  as.factor(ROWID), data=D3.VIOL) # rape
S1C.VIOL <- cl(dat=D3.VIOL, fm=S1.VIOL, cluster=D3.VIOL$VILLID)

S1.DV <- lm(ALLOUT.DV ~ as.factor(OUTTYPE.DV)+  as.factor(ROWID), data=D3.DV) # wife-beating
S1C.DV <- cl(dat=D3.DV, fm=S1.DV, cluster=D3.DV$VILLID)

S1.VOL <- lm(ALLOUT.VOL ~ as.factor(OUTTYPE.VOL)+  as.factor(ROWID), data=D3.VOL) # theft
S1C.VOL <- cl(dat=D3.VOL, fm=S1.VOL, cluster=D3.VOL$VILLID)

## Adding fixed effects for respondent sex, removing individual fixed effects
S2.VIOL <- lm(ALLOUT.VIOL ~ as.factor(OUTTYPE.VIOL) + as.factor(FEMALE), data=D3.VIOL)
S2C.VIOL <- cl(dat=D3.VIOL, fm=S2.VIOL, cluster=D3.VIOL$VILLID)

S2.DV <- lm(ALLOUT.DV ~ as.factor(OUTTYPE.DV) + as.factor(FEMALE), data=D3.DV)
S2C.DV <- cl(dat=D3.DV, fm=S2.DV, cluster=D3.DV$VILLID)

S2.VOL <- lm(ALLOUT.VOL ~ as.factor(OUTTYPE.VOL) + as.factor(FEMALE), data=D3.VOL)
S2C.VOL <- cl(dat=D3.VOL, fm=S2.VOL, cluster=D3.VOL$VILLID)

## Adding interaction term with sex, removing individual fixed effects
S3.VIOL <- lm(ALLOUT.VIOL ~ as.factor(OUTTYPE.VIOL)*as.factor(FEMALE), data=D3.VIOL)
S3C.VIOL <- cl(dat=D3.VIOL, fm=S3.VIOL, cluster=D3.VIOL$VILLID)

S3.DV <- lm(ALLOUT.DV ~ as.factor(OUTTYPE.DV)*as.factor(FEMALE), data=D3.DV)
S3C.DV <- cl(dat=D3.DV, fm=S3.DV, cluster=D3.DV$VILLID)

S3.VOL <- lm(ALLOUT.VOL ~ as.factor(OUTTYPE.VOL)*as.factor(FEMALE), data=D3.VOL)
S3C.VOL <- cl(dat=D3.VOL, fm=S3.VOL, cluster=D3.VOL$VILLID)

## Adding individual fixed effects back in
S4.VIOL <- lm(ALLOUT.VIOL ~ as.factor(OUTTYPE.VIOL)*as.factor(FEMALE) + as.factor(ROWID), data=D3.VIOL)
S4C.VIOL <- cl(dat=D3.VIOL, fm=S4.VIOL, cluster=D3.VIOL$VILLID)

S4.DV <- lm(ALLOUT.DV ~ as.factor(OUTTYPE.DV)*as.factor(FEMALE) + as.factor(ROWID), data=D3.DV)
S4C.DV <- cl(dat=D3.DV, fm=S4.DV, cluster=D3.DV$VILLID)

S4.VOL <- lm(ALLOUT.VOL ~ as.factor(OUTTYPE.VOL)*as.factor(FEMALE) + as.factor(ROWID), data=D3.VOL)
S4C.VOL <- cl(dat=D3.VOL, fm=S4.VOL, cluster=D3.VOL$VILLID)

## stargazer function to format tables A.6-A.8
starfct <- function(A, B, C, D, AC, BC, CC, DC, TI){stargazer(A, B, C, D,type = "text", 
                                                              title = TI, 
                                                              se = list(AC[,2], BC[,2], CC[,2], DC[,2]), 
                                                              t = list(AC[,3], BC[,3], CC[,3], DC[,3]), 
                                                              p = list(AC[,4], BC[,4], CC[,4], DC[,4]), 
                                                              omit.stat = c("ser", "f","rsq"),
                                                              dep.var.labels = c("Stacked Outcomes"),
                                                              dep.var.caption  = "Dependent Variable: Punishment Preferences",
                                                              font.size = "small",
                                                              column.labels   = c("Ind.FEs", "Female.Ctrl","Female.Het", "All"),
                                                              #column.separate = c(3, 3),
                                                              notes = "Standard Errors clustered at the village level (20 villages)",
                                                              #intercept.bottom = FALSE,
                                                              omit = c("ROWID"),
                                                              omit.labels=c("Indiv. Fixed Effects?"),
                                                              no.space = TRUE,
                                                              covariate.labels = c("Public (base=Private)",
                                                                                   "Group (base=Private)",
                                                                                   "PostDisc Private (base=Private)",
                                                                                   "Female",
                                                                                   "Female x Public (base=Private)",
                                                                                   "Female x Group (base=Private)",
                                                                                   "Female x PostDisc Private (base=Private)"),
                                                              header=FALSE) 
}

############################################################################
# Table A.6: Private versus Group Outcomes: Rape
############################################################################

starfct(A=S1.VIOL, B=S2.VIOL, C=S3.VIOL, D=S4.VIOL, 
        AC=S1C.VIOL, BC=S2C.VIOL, CC=S3C.VIOL, DC=S4C.VIOL, 
        TI="Private versus Group Outcomes: Rape")

############################################################################
# Table A.7: Private versus Group Outcomes: Wifebeating
############################################################################

starfct(A=S1.DV, B=S2.DV, C=S3.DV, D=S4.DV, 
        AC=S1C.DV, BC=S2C.DV, CC=S3C.DV, DC=S4C.DV, 
        TI="Private versus Group Outcomes: Wifebeating")

############################################################################
# Table A.8: Private versus Group Outcomes: Theft
############################################################################

starfct(A=S1.VOL, B=S2.VOL, C=S3.VOL, D=S4.VOL, 
        AC=S1C.VOL, BC=S2C.VOL, CC=S3C.VOL, DC=S4C.VOL, 
        TI="Private versus Group Outcomes: Theft")



############################################################################
# Table A.9: Determinants of Post Discussion Private Preferences
############################################################################

#This is the full output for the Table 3 in the main paper
#Regression analyses appears in code for Table 3
#cluster function also appears in code for Table 3 

#function for divided data regression output
starfcti <- function(A, B, C, AC, BC, CC, TI){stargazer(A, B, C, type = "text", 
                                                        title = TI, 
                                                        se = list(AC[,2], BC[,2], CC[,2]), 
                                                        t = list(AC[,3], BC[,3], CC[,3]), 
                                                        p = list(AC[,4], BC[,4], CC[,4]), 
                                                        omit.stat = c("ser", "f"),
                                                        dep.var.labels = c("Rape","Wifebeating", "Theft"),
                                                        dep.var.caption  = "Dependent Variable: Punishment Preferences",
                                                        font.size = "small",
                                                        keep=c("PRIV1.RC2.VIOL", "GCHOIX.RC2.VIOL","PRIV1.RC2.DV", "GCHOIX.RC2.DV","PRIV1.RC2.VOL", "GCHOIX.RC2.VOL", "PRIV1.RC2.VIOL:GCHOIX.RC2.VIOL", "PRIV1.RC2.DV:GCHOIX.RC2.DV", "PRIV1.RC2.VOL:GCHOIX.RC2.VOL", "FEMALE", "EDU", "RENCONAN", "HOMOGENE", "VIVEVILLN", "GUERREAPRES2011", "LEVIOL.FG", "LAVIOLENCEDOMESTIQUE.FG", "LEVOL.FG"),
                                                        omit = c("VFGID3"),
                                                        #omit = c("VFGID3", "EDU"),
                                                        #omit.labels=c("Focus Group Fixed Effects? (N=79)", "Controls?"),
                                                        omit.labels=c("Focus Group Fixed Effects? (N=79)"),
                                                        no.space=TRUE,
                                                        notes = "Standard Errors clustered at the Village level (20)",
                                                        #intercept.bottom = FALSE,
                                                        order = c("PRIV1.RC2.VIOL", "GCHOIX.RC2.VIOL","PRIV1.RC2.DV", "GCHOIX.RC2.DV","PRIV1.RC2.VOL", "GCHOIX.RC2.VOL", "PRIV1.RC2.VIOL:GCHOIX.RC2.VIOL", "PRIV1.RC2.DV:GCHOIX.RC2.DV", "PRIV1.RC2.VOL:GCHOIX.RC2.VOL", "FEMALE", "EDU", "RENCONAN", "HOMOGENE", "VIVEVILLN", "GUERREAPRES2011", "LEVIOL.FG", "LAVIOLENCEDOMESTIQUE.FG", "LEVOL.FG"),
                                                        covariate.labels = c("Rape: Private", "Rape: Private x Group", "Rape: Group", "DV: Private", "DV: Private x Group", "DV: Group", "Theft: Private", "Theft: Private x Group", "Theft: Group", "Female", "Education", "Freq. of Meeting others", "Homogenous Subgroup", "Years in Village (Categorical)", "Armed Conflict in Village (5 yrs)", "Crime Order: Rape", "Crime Order: DV", "Crime Order: Theft"),
                                                        header=FALSE)
}

# regression output
starfcti(A=I2.VIOL, B=I2.DV, C=I2.VOL, 
         AC=I2C.VIOL, BC=I2C.DV, CC=I2C.VOL, 
         TI="Determinants of Post Discussion Private Preferences")

############################################################################
# Table A.10: Determinants of Post Discussion Private Preferences (Divided Data)
############################################################################

#sex disaggregated analysis of the effects of private preferences and group choice 
#on post-discussion private preferences
#cluster function (cl) appears along with code for Table 3 

# regressions for men only
#rape
I2M.VIOL <- lm(PRIV2.RC2.VIOL ~ PRIV1.RC2.VIOL* GCHOIX.RC2.VIOL + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVIOL.FG + GUERREAPRES2011 + as.factor(VFGID3), data=D3[D3$FEMALE==0,])
I2MC.VIOL <- cl(dat=D3[D3$FEMALE==0,], fm=I2M.VIOL, cluster=D3$VILLID[D3$FEMALE==0])

#wife-beating
I2M.DV <- lm(PRIV2.RC2.DV ~ PRIV1.RC2.DV*GCHOIX.RC2.DV + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LAVIOLENCEDOMESTIQUE.FG + GUERREAPRES2011 + as.factor(VFGID3), data=D3[D3$FEMALE==0,])
I2MC.DV <- cl(dat=D3[D3$FEMALE==0,], fm=I2M.DV, cluster=D3$VILLID[D3$FEMALE==0])

#theft
I2M.VOL <- lm(PRIV2.RC2.VOL ~ PRIV1.RC2.VOL*GCHOIX.RC2.VOL + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVOL.FG + GUERREAPRES2011 + as.factor(VFGID3), data=D3[D3$FEMALE==0,])
I2MC.VOL <- cl(dat=D3[D3$FEMALE==0,], fm=I2M.VOL, cluster=D3$VILLID[D3$FEMALE==0])

#regressions for women only

#rape
I2F.VIOL <- lm(PRIV2.RC2.VIOL ~ PRIV1.RC2.VIOL* GCHOIX.RC2.VIOL + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVIOL.FG + GUERREAPRES2011 + as.factor(VFGID3), data=D3[D3$FEMALE==1,])
I2FC.VIOL <- cl(dat=D3[D3$FEMALE==1,], fm=I2F.VIOL, cluster=D3$VILLID[D3$FEMALE==1])

#wife-beating
I2F.DV <- lm(PRIV2.RC2.DV ~ PRIV1.RC2.DV*GCHOIX.RC2.DV + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LAVIOLENCEDOMESTIQUE.FG + GUERREAPRES2011 + as.factor(VFGID3), data=D3[D3$FEMALE==1,])
I2FC.DV <- cl(dat=D3[D3$FEMALE==1,], fm=I2F.DV, cluster=D3$VILLID[D3$FEMALE==1])

#theft
I2F.VOL <- lm(PRIV2.RC2.VOL ~ PRIV1.RC2.VOL*GCHOIX.RC2.VOL + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVOL.FG + GUERREAPRES2011 + as.factor(VFGID3), data=D3[D3$FEMALE==1,])
I2FC.VOL <- cl(dat=D3[D3$FEMALE==1,], fm=I2F.VOL, cluster=D3$VILLID[D3$FEMALE==1])

#function for divided data regression output
starfctediv <- function(A, B, C, D, E, G, AC, BC, CC, DC, EC, GC, TI){stargazer(A, B, C, D, E, G, type = "text", 
                                                                                title = TI, 
                                                                                se = list(AC[,2], BC[,2], CC[,2], DC[,2], EC[,2], GC[,2]), 
                                                                                t = list(AC[,3], BC[,3], CC[,3], DC[,3], EC[,3], GC[,3]), 
                                                                                p = list(AC[,4], BC[,4], CC[,4], DC[,4], EC[,4], GC[,4]), 
                                                                                omit.stat = c("ser", "f"),
                                                                                dep.var.labels = c("Rape", "Wifebeating", "Theft"),
                                                                                dep.var.caption  = "Dependent Variable: Punishment Preferences",
                                                                                font.size = "small",
                                                                                keep=c("PRIV1.RC2.VIOL", "GCHOIX.RC2.VIOL","PRIV1.RC2.DV", "GCHOIX.RC2.DV","PRIV1.RC2.VOL", "GCHOIX.RC2.VOL", "PRIV1.RC2.VIOL:GCHOIX.RC2.VIOL", "PRIV1.RC2.DV:GCHOIX.RC2.DV", "PRIV1.RC2.VOL:GCHOIX.RC2.VOL", "EDU", "RENCONAN", "HOMOGENE", "VIVEVILLN", "GUERREAPRES2011", "LEVIOL.FG", "LAVIOLENCEDOMESTIQUE.FG", "LEVOL.FG"),
                                                                                omit = c("VFGID3"),
                                                                                omit.labels=c("Focus Group Fixed Effects? (N=79)"),
                                                                                no.space=TRUE,
                                                                                column.labels   = c("Men", "Women","Men", "Women","Men", "Women"),
                                                                                #column.separate = c(3, 3),
                                                                                notes = "Standard Errors clustered at the Village level (20)",
                                                                                #intercept.bottom = FALSE,
                                                                                order=c("PRIV1.RC2.VIOL", "GCHOIX.RC2.VIOL","PRIV1.RC2.DV", "GCHOIX.RC2.DV","PRIV1.RC2.VOL", "GCHOIX.RC2.VOL", "PRIV1.RC2.VIOL:GCHOIX.RC2.VIOL", "PRIV1.RC2.DV:GCHOIX.RC2.DV", "PRIV1.RC2.VOL:GCHOIX.RC2.VOL", "EDU", "RENCONAN", "HOMOGENE", "VIVEVILLN", "GUERREAPRES2011", "LEVIOL.FG", "LAVIOLENCEDOMESTIQUE.FG", "LEVOL.FG"),
                                                                                covariate.labels = c("Rape: Private", "Rape: Private x Group", "Rape: Group", "DV: Private", "DV: Private x Group", "DV: Group", "Theft: Private", "Theft: Private x Group", "Theft: Group", "Education", "Freq. of Meeting others", "Homogenous Subgroup", "Years in Village (Categorical)", "Armed Conflict in Village (5 yrs)", "Crime Order: Rape", "Crime Order: DV", "Crime Order: Theft"),
                                                                                header=FALSE) 
}

# regression output
starfctediv(A=I2M.VIOL, B=I2F.VIOL, C=I2M.DV, D=I2F.DV, E=I2M.VOL, G=I2F.VOL,
            AC=I2MC.VIOL, BC=I2FC.VIOL, CC=I2MC.DV, DC=I2FC.DV, EC=I2MC.VOL, GC=I2FC.VOL,
            TI="Determinants of Post Discussion Private Preferences (Divided Data)")


############################################################################
# Calculations Tables A.11-A.13
# Determinants of Preference Convergence
############################################################################
# Uses preference convergence variableS "ALLDIST.VIOL", "ALLDIST.DV", OR "ALLDIST.VOL"
# As calculated within the code for creating Figure 8 in the Main Paper

# Regressions with fixed effects by outcome preference type
# with standard errors clustered by village
# cluster function (cl) appears along with code for Table 3

## Individual fixed effects
A1DIST.VIOL <- lm(ALLDIST.VIOL ~ OUTTYPE.VIOL + as.factor(ROWID2), data=D3DIST.VIOL) # rape
A1CDIST.VIOL <- cl(dat=D3DIST.VIOL, fm=A1DIST.VIOL, cluster=D3DIST.VIOL$VILLID)

A1DIST.DV <- lm(ALLDIST.DV ~ OUTTYPE.DV + as.factor(ROWID2), data=D3DIST.DV) # wife-beating
A1CDIST.DV <- cl(dat=D3DIST.DV, fm=A1DIST.DV, cluster=D3DIST.DV$VILLID)

A1DIST.VOL <- lm(ALLDIST.VOL ~ OUTTYPE.VOL + as.factor(ROWID2), data=D3DIST.VOL) # theft
A1CDIST.VOL <- cl(dat=D3DIST.VOL, fm=A1DIST.VOL, cluster=D3DIST.VOL$VILLID)


## Adding interaction term with respondent sex
A4DIST.VIOL <- lm(ALLDIST.VIOL ~ OUTTYPE.VIOL*FEMALE + as.factor(ROWID2), data=D3DIST.VIOL)
A4CDIST.VIOL <- cl(dat=D3DIST.VIOL, fm=A4DIST.VIOL, cluster=D3DIST.VIOL$VILLID)

A4DIST.DV <- lm(ALLDIST.DV ~ OUTTYPE.DV*FEMALE + as.factor(ROWID2), data=D3DIST.DV)
A4CDIST.DV <- cl(dat=D3DIST.DV, fm=A4DIST.DV, cluster=D3DIST.DV$VILLID)

A4DIST.VOL <- lm(ALLDIST.VOL ~ OUTTYPE.VOL*FEMALE + as.factor(ROWID2), data=D3DIST.VOL)
A4CDIST.VOL <- cl(dat=D3DIST.VOL, fm=A4DIST.VOL, cluster=D3DIST.VOL$VILLID)


## Full set of control variables with no individual fixed effects
A2DIST.VIOL <- lm(ALLDIST.VIOL ~ OUTTYPE.VIOL + FEMALE + GUERREAPRES2011 + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVIOL.FG, data=D3DIST.VIOL)
A2CDIST.VIOL <- cl(dat=D3DIST.VIOL, fm=A2DIST.VIOL, cluster=D3DIST.VIOL$VILLID)

A2DIST.DV <- lm(ALLDIST.DV ~ OUTTYPE.DV + FEMALE + GUERREAPRES2011 + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LAVIOLENCEDOMESTIQUE.FG, data=D3DIST.DV)
A2CDIST.DV <- cl(dat=D3DIST.DV, fm=A2DIST.DV, cluster=D3DIST.DV$VILLID)

A2DIST.VOL <- lm(ALLDIST.VOL ~ OUTTYPE.VOL + FEMALE + GUERREAPRES2011 + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVOL.FG, data=D3DIST.VOL)
A2CDIST.VOL <- cl(dat=D3DIST.VOL, fm=A2DIST.VOL, cluster=D3DIST.VOL$VILLID)

## Full set of control variables with interaction term between respondent sex and preference type
A3DIST.VIOL <- lm(ALLDIST.VIOL ~ OUTTYPE.VIOL*FEMALE + GUERREAPRES2011 + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVIOL.FG, data=D3DIST.VIOL)
A3CDIST.VIOL <- cl(dat=D3DIST.VIOL, fm=A3DIST.VIOL, cluster=D3DIST.VIOL$VILLID)

A3DIST.DV <- lm(ALLDIST.DV ~ OUTTYPE.DV*FEMALE + GUERREAPRES2011 + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LAVIOLENCEDOMESTIQUE.FG, data=D3DIST.DV)
A3CDIST.DV <- cl(dat=D3DIST.DV, fm=A3DIST.DV, cluster=D3DIST.DV$VILLID)

A3DIST.VOL <- lm(ALLDIST.VOL ~ OUTTYPE.VOL*FEMALE + GUERREAPRES2011 + EDU + RENCONAN + HOMOGENE + VIVEVILLN + LEVOL.FG, data=D3DIST.VOL)
A3CDIST.VOL <- cl(dat=D3DIST.VOL, fm=A3DIST.VOL, cluster=D3DIST.VOL$VILLID)


## function to generate table of regression outputs
starfct.DIST <- function(A, B, C, D, AC, BC, CC, DC, TI){stargazer(A, B, C, D, type = "text", 
                                                                   title = TI, 
                                                                   se = list(AC[,2], BC[,2], CC[,2], DC[,2]), 
                                                                   t = list(AC[,3], BC[,3], CC[,3], DC[,3]), 
                                                                   p = list(AC[,4], BC[,4], CC[,4], DC[,4]), 
                                                                   omit.stat = c("ser", "f","rsq"),
                                                                   dep.var.labels = c("Stacked Outcomes"),
                                                                   dep.var.caption  = "Dependent Variable: Distance from Mean FG Preference",
                                                                   font.size = "small",
                                                                   column.labels   = c("Ind.FEs", "All.Ctrl", "Female.Het.1", "Female.Het.2"),
                                                                   #column.separate = c(3, 3),
                                                                   notes = "Standard Errors clustered at the village level (20 villages)",
                                                                   #intercept.bottom = FALSE,
                                                                   omit = c("ROWID2"),
                                                                   omit.labels=c("Indiv. Fixed Effects?"),
                                                                   no.space = TRUE,
                                                                   covariate.labels = c("PostDisc Private (base=Private)",
                                                                                        "Female x PostDisc Private (base=Private)",
                                                                                        "Female", "Armed Conflict in Village (5 yrs)", "Education", "Freq. of Meeting others", "Homogenous Subgroup", "Years in Village (Categorical)", "Crime Order"),
                                                                   header=FALSE) 
}


############################################################################
# Table A.11
############################################################################

starfct.DIST(A=A1DIST.VIOL, B=A2DIST.VIOL, C=A4DIST.VIOL, D=A3DIST.VIOL, 
             AC=A1CDIST.VIOL, BC=A2CDIST.VIOL, CC=A4CDIST.VIOL, DC=A3CDIST.VIOL, 
             TI="Private versus Post Distance: Rape")

############################################################################
# Table A.12
############################################################################

starfct.DIST(A=A1DIST.DV, B=A2DIST.DV, C=A4DIST.DV, D=A3DIST.DV, 
             AC=A1CDIST.DV, BC=A2CDIST.DV, CC=A4CDIST.DV, DC=A3CDIST.DV, 
             TI="Private versus Post Distance: Wifebeating")

############################################################################
# Table A.13
############################################################################

starfct.DIST(A=A1DIST.VOL, B=A2DIST.VOL, C=A4DIST.VOL, D=A3DIST.VOL, 
             AC=A1CDIST.VOL, BC=A2CDIST.VOL, CC=A4CDIST.VOL, DC=A3CDIST.VOL, 
             TI="Private versus Post Distance: Theft")


